package com.boomsecret.juc.executor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author wangjinliang on 2019/1/22.
 */
public class FutureDemo {
    private static ExecutorService threadPool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());

    public static void main(String[] args) {
        Future<String> result = threadPool.submit(() -> {
            System.out.println("thread start.");
            TimeUnit.SECONDS.sleep(8);
            System.out.println("thread end.");
            return "success";
        });
        try {
            TimeUnit.SECONDS.sleep(1);
//            System.out.println(result.get());
//            System.out.println(result.get(1, TimeUnit.SECONDS));
        } catch (Exception e) {
            e.printStackTrace();
        }
//        System.out.println(result.cancel(true));
//        System.out.println(result.cancel(false));
        System.out.println(result.isCancelled());
//        System.out.println(result.isDone());
        System.out.println("主线程执行完毕");
        threadPool.shutdown();
    }
}
